﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:CodeGenerator="http://sapura.com.my/codegenerator"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl">

    <xsl:output method="xml"
                indent="yes"
                encoding="utf-8"
                standalone="yes"
                omit-xml-declaration="yes" />

    <xsl:template match="Metadata" >
<data description="multiple services per each table"
        enableBatchRequests="false"
        enableBoxcarring="false"
        serviceNamespace="http://ws.wso2.org/dataservice"
        serviceStatus="active">
        <xsl:attribute name="name"><xsl:value-of select="@Name"/></xsl:attribute>
    <config id="default">
        <property name="carbon_datasource_name">UC2_DDS</property>
    </config>
    <xsl:apply-templates />
</data>
<xsl:value-of select="CodeGenerator:Save('DataServices', concat(@Name, '.dbs'))" />
</xsl:template>

<xsl:template match="Object[@IsSelected='True']">
    <query useConfig="default" is-generated="true">
        <xsl:attribute name="id">update_<xsl:value-of select="@Name"  />_query</xsl:attribute>
        <sql>UPDATE public.<xsl:value-of select="@Name"  /> SET <xsl:for-each select="Property[@IsPrimaryKey=0]"><xsl:value-of select="@Name"/>=?<xsl:if test="position()!=last()">, </xsl:if></xsl:for-each> WHERE Id=?</sql>
        <xsl:for-each select="Property">
        <param ordinal="1"
                paramType="SCALAR"
                type="IN">
            <xsl:attribute name="name"><xsl:value-of select="@Name" /></xsl:attribute>
            <xsl:attribute name="ordinal"><xsl:value-of select="@OrdinalPosition" /></xsl:attribute>
            <xsl:attribute name="sqlType"><xsl:call-template name="SqlDataType" /></xsl:attribute>
        </param>
        </xsl:for-each>
    </query>
    <query useConfig="default">
        <xsl:attribute name="id">select_all_<xsl:value-of select="@Name"  />_query</xsl:attribute>
        <sql>SELECT <xsl:for-each select="Property"><xsl:value-of select="@Name"/><xsl:if test="position()!=last()">, </xsl:if></xsl:for-each> FROM public.<xsl:value-of select="@Name"  /></sql>
        <result>
        <xsl:attribute name="element"><xsl:value-of select="@Name"  />Collection</xsl:attribute>
        <xsl:attribute name="rowName"><xsl:value-of select="@Name"  /></xsl:attribute>
        <xsl:for-each select="Property">
            <element>
                <xsl:attribute name="column"><xsl:value-of select="@Name" /></xsl:attribute>
                <xsl:attribute name="name"><xsl:value-of select="@Name" /></xsl:attribute>
                <xsl:attribute name="xsdType"><xsl:call-template name="XcDataType" /></xsl:attribute>
            </element>
        </xsl:for-each>
        </result>
    </query>
    <query useConfig="default">
        <xsl:attribute name="id">insert_<xsl:value-of select="@Name"  />_query</xsl:attribute>
        <sql>INSERT INTO public.<xsl:value-of select="@Name"  />(<xsl:for-each select="Property"><xsl:value-of select="@Name"/><xsl:if test="position()!=last()">, </xsl:if></xsl:for-each>) VALUES(<xsl:for-each select="Property">?<xsl:if test="position()!=last()">, </xsl:if></xsl:for-each>)</sql>
        <xsl:for-each select="Property">
        <param ordinal="1"
                paramType="SCALAR"
                type="IN">
            <xsl:attribute name="name"><xsl:value-of select="@Name" /></xsl:attribute>
            <xsl:attribute name="ordinal"><xsl:value-of select="@OrdinalPosition" /></xsl:attribute>
            <xsl:attribute name="sqlType"><xsl:call-template name="SqlDataType" /></xsl:attribute>
        </param>
        </xsl:for-each>
    </query>
    <query useConfig="default">
        <xsl:attribute name="id">select_with_key_<xsl:value-of select="@Name"  />_query</xsl:attribute>
        <sql>SELECT <xsl:for-each select="Property"><xsl:value-of select="@Name"/><xsl:if test="position()!=last()">, </xsl:if></xsl:for-each> FROM public.<xsl:value-of select="@Name" /> WHERE Id=?</sql>
        <param name="Id"
                ordinal="1"
                paramType="SCALAR"
                sqlType="INTEGER"
                type="IN"/>
        <result>
        <xsl:attribute name="element"><xsl:value-of select="@Name"  />Collection</xsl:attribute>
        <xsl:attribute name="rowName"><xsl:value-of select="@Name"  /></xsl:attribute>
        <xsl:for-each select="Property">
            <element>
                <xsl:attribute name="column"><xsl:value-of select="@Name" /></xsl:attribute>
                <xsl:attribute name="name"><xsl:value-of select="@Name" /></xsl:attribute>
                <xsl:attribute name="xsdType"><xsl:call-template name="XcDataType" /></xsl:attribute>
            </element>
        </xsl:for-each>
        </result>
    </query>
    <query useConfig="default">
        <xsl:attribute name="id">delete_<xsl:value-of select="@Name"  />_query</xsl:attribute>
        <sql>DELETE FROM public.<xsl:value-of select="@Name"  /> WHERE Id=?</sql>
        <param name="Id"
                ordinal="1"
                paramType="SCALAR"
                sqlType="INTEGER"
                type="IN"/>
    </query>
    <operation>
        <xsl:attribute name="name">SelectAll<xsl:value-of select="@Name"  />Operation</xsl:attribute>
        <call-query><xsl:attribute name="href">select_all_<xsl:value-of select="@Name"  />_query</xsl:attribute></call-query>
    </operation>
    <operation>
        <xsl:attribute name="name">SelectWithKey<xsl:value-of select="@Name"  />Operation</xsl:attribute>
        <call-query>
            <xsl:attribute name="href">select_with_key_<xsl:value-of select="@Name"  />_query</xsl:attribute>
            <with-param name="Id"
                        query-param="Id"/>
        </call-query>
    </operation>
    <operation>
        <xsl:attribute name="name">Insert<xsl:value-of select="@Name"  />Operation</xsl:attribute>
        <call-query>
            <xsl:attribute name="href">insert_<xsl:value-of select="@Name"  />_query</xsl:attribute>
            <xsl:for-each select="Property">
            <with-param>
                <xsl:attribute name="name"><xsl:value-of select="@Name" /></xsl:attribute>
                <xsl:attribute name="query-param"><xsl:value-of select="@Name" /></xsl:attribute>
            </with-param>
            </xsl:for-each>
        </call-query>
    </operation>
    <operation>
        <xsl:attribute name="name">Delete<xsl:value-of select="@Name"  />Operation</xsl:attribute>
        <call-query>
            <xsl:attribute name="href">delete_<xsl:value-of select="@Name"  />_query</xsl:attribute>
            <with-param name="Id"
                        query-param="Id"/>
        </call-query>
    </operation>
    <operation>
        <xsl:attribute name="name">Update<xsl:value-of select="@Name"  />Operation</xsl:attribute>
        <call-query>
            <xsl:attribute name="href">update_<xsl:value-of select="@Name"  />_query</xsl:attribute>
            <xsl:for-each select="Property">
            <with-param>
                <xsl:attribute name="name"><xsl:value-of select="@Name" /></xsl:attribute>
                <xsl:attribute name="query-param"><xsl:value-of select="@Name" /></xsl:attribute>
            </with-param>
            </xsl:for-each>
        </call-query>
    </operation>
</xsl:template>

<xsl:template name="XcDataType">xs:<xsl:choose><xsl:when test="@DataType='character varying'">string</xsl:when><xsl:when test="@DataType='character'">string</xsl:when><xsl:when test="@DataType='bytea'">base64</xsl:when><xsl:when test="@DataType='xml'">string</xsl:when><xsl:when test="@DataType='text'">string</xsl:when><xsl:when test="@DataType='timestamp with time zone'">dateTime</xsl:when><xsl:when test="@DataType='timestamp without time zone'">dateTime</xsl:when><xsl:otherwise><xsl:value-of select="@DataType" /></xsl:otherwise></xsl:choose></xsl:template>

<xsl:template name="SqlDataType"><xsl:choose><xsl:when test="@DataType='character varying'">STRING</xsl:when><xsl:when test="@DataType='character'">STRING</xsl:when><xsl:when test="@DataType='bytea'">BINARY</xsl:when><xsl:when test="@DataType='xml'">STRING</xsl:when><xsl:when test="@DataType='xml'">STRING</xsl:when><xsl:when test="@DataType='text'">STRING</xsl:when><xsl:when test="@DataType='timestamp with time zone'">TIMESTAMP</xsl:when><xsl:when test="@DataType='timestamp without time zone'">TIMESTAMP</xsl:when><xsl:otherwise><xsl:value-of select="translate(@DataType,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" /></xsl:otherwise></xsl:choose></xsl:template>

</xsl:stylesheet>